﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices.ComTypes;
using System.IO;

#if WPF
using System.Windows;
using Silverlight.Hosting.Wpf.Interop;
using Silverlight.Hosting.Wpf.Private;
using Color = System.Windows.Media.Color;

namespace Silverlight.Hosting.Wpf
#else
using Silverlight.Hosting.Interop;
using Silverlight.Hosting.Private;

namespace Silverlight.Hosting
#endif
{
    /// <summary>
    /// Describes a Silverlight plug-in host.
    /// </summary>
    public interface ISilverlightHost : ISilverlightHostEventProvider
    {
        #region Events

        /// <summary>
        /// Raised when the Silverlight control is created.
        /// </summary>
#if WPF
        event RoutedEventHandler Created;
#else
        event EventHandler Created;
#endif

        /// <summary>
        /// Raised whenever the XAML DOM has successfully loaded (i.e. the application
        /// is fully loaded and ready to display). This notification is useful because
        /// loading Silverlight can take longer for complex applications and when loading
        /// the CLR for the first time. 
        /// </summary>
#if WPF
        event RoutedEventHandler SilverlightLoaded;
#else
        event EventHandler SilverlightLoaded;
#endif
        /// <summary>
        /// Raised whenever errors are raised from within the XAML DOM of the hosted control.
        /// This event can indicate when a load failure has occurred, for example, when the
        /// expected .xap file does not exist. You can also use this event to respond to XAML
        /// parsing errors and runtime errors while using the JavaScript API for Silverlight.
        /// </summary>
#if WPF
        event RoutedEventHandler<SilverlightErrorEventArgs> SilverlightError;
#else
        event EventHandler<SilverlightErrorEventArgs> SilverlightError;
#endif

        /// <summary>
        /// Raised whenever events occur within the object model of the hosted control.
        /// </summary>
#if WPF
        event RoutedEventHandler<SilverlightEventArgs> SilverlightEvent;
#else
        event EventHandler<SilverlightEventArgs> SilverlightEvent;
#endif


        /// <summary>
        /// Raised when the source download has finished.
        /// </summary>
#if WPF
        event RoutedEventHandler SourceDownloadComplete;
#else
        event EventHandler SourceDownloadComplete;
#endif

        /// <summary>
        /// Raised when the source download progress changes.
        /// </summary>
#if WPF
        event RoutedEventHandler<SourceDownloadProgressChangedEventArgs> SourceDownloadProgressChanged;
#else
        event EventHandler<SourceDownloadProgressChangedEventArgs> SourceDownloadProgressChanged;
#endif

        #endregion

        #region Properties

        /// <summary>
        /// Gets a value indicating whether the control is windowless.
        /// </summary>
        bool IsWindowless { get; }

        /// <summary>
        /// Gets a value indicating whether the control has been created.
        /// </summary>
        bool IsCreated { get; }

        /// <summary>
        /// Gets or sets the URL that the hosted control should use as the base for any relative requests made for Source,
        /// to the downloader, etc.
        /// </summary>
        Uri BaseUrl { get; set; }

        /// <summary>
        /// Gets or sets the XcpHostOptions specified by the host.
        /// </summary>
        XcpHostOptions HostOptions { get; set; }

        /// <summary>
        /// Gets or sets the version of the intalled Silverlight plug-in.
        /// </summary>
        Version Version { get; set; }

        /// <summary>
        /// Gets or sets the URI specified for the source content (can be a XAML page or a package, depending on
        /// which programming model is used for the Silverlight content).
        /// </summary>
        Uri Source { get; set; }

        /// <summary>
        /// Gets or sets the XAML content dispayed in the Silverlight plug-in.
        /// </summary>
        string Xaml { get; set; }

        /// <summary>
        /// Gets a Boolean that declares whether the control's onLoad event has been raised.
        /// </summary>
        bool IsLoaded { get; }

        /// <summary>
        /// Gets the content sub-object.
        /// </summary>
        Content Content { get; }

        /// <summary>
        /// Gets the settings sub-object.
        /// </summary>
        Settings Settings { get; }

        /// <summary>
        /// Gets or sets initialization parameters.
        /// </summary>
        string InitParams { get; set; }

        /// <summary>
        /// Gets or sets the URI specified for the splash screen source XAML.
        /// </summary>
        Uri SplashScreenSource { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Loads the CLR runtime
        /// </summary>
        void LoadRuntime();

        /// <summary>
        /// Creates a new instance of a Silverlight helper object.
        /// </summary>
        /// <param name="objectType">Specifies the type of object to create.</param>
        /// <returns>The resulting object.</returns>
        SafeComObject<IXcpObject> CreateObject(string objectType);

        /// <summary>
        /// Determines whether the existing Silverlight plug-in
        /// is a matching version or is a compatible version with a specified version string.
        /// </summary>
        /// <param name="version">String specifying the version to check.</param>
        /// <returns>True if the version is supported; otherwise, False.</returns>
        bool IsVersionSupported(string version);

        #endregion
    }

#if WPF
    /// <summary>
    /// Represents the method that will handle various routed events that do not
    /// have specific event data beyond the data that is common for all routed events.
    /// </summary>
    /// <typeparam name="T">The event type.</typeparam>
    /// <param name="sender">The object where the event handler is attached.</param>
    /// <param name="e">The event data.</param>
    public delegate void RoutedEventHandler<T>(object sender, T e) where T : RoutedEventArgs;
#endif

    /// <summary>
    /// Describes the result of the download operation.
    /// </summary>
    public enum DownloadResult
    {
        /// <summary>
        /// The download was handled successfully.
        /// </summary>
        OK,
        /// <summary>
        /// The download is being handled asynchronously.
        /// </summary>
        Pending,
        /// <summary>
        /// Silverlight should handle the download.
        /// </summary>
        Default
    }

    [Flags]
    public enum XcpHostOptions : uint
    {
        Default =
            EnableCrossDomainDownloads |
            EnableScriptableObjectAccess |
            EnableHtmlDomAccess |
            EnableScriptableObjectAccess |
            EnableAssemblySharing |
            HookGetComAutomationObject |
            EnableElevatedPermissions |
            EnableWindowlessAccessibility,

        FreezeOnInitialFrame = 0x01,
        DisableFullScreen = 0x02,
        DisableManagedExecution = 0x08,
        EnableCrossDomainDownloads = 0x10,
        UseCustomAppDomain = 0x020,
        DisableNetworking = 0x040,
        DisableScriptCallouts = 0x080,
        EnableHtmlDomAccess = 0x100,
        EnableScriptableObjectAccess = 0x200,
        // Skipping a number so as not to conflict with mobile.
        EnableAssemblySharing = 0x800,
        HookGetComAutomationObject = 0x1000,
        EnableElevatedPermissions = 0x2000,
        EnableWindowlessAccessibility = 0x4000,
    }
}
